Completed
Push — master ( 4893ea...179eb0 )
by Askupa
02:44
created

Amarkal.settings._updateValues   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
/**
2
 * Asynchronously save all settings in the current settings page to the database.
3
 * Shows an error notification if errors occur. Shows a success notification
4
 * otherwise.
5
 * 
6
 * @param {Function} done
7
 */
8
Amarkal.settings.save = function( done ) 
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
{
10
    Amarkal.settings._postData('save',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
        
12
        if(res.errors.length) {
13
            var error = '';
14
            for(var i = 0; i < res.errors.length; i++) {
15
                error += res.errors[i];
16
            }
17
            Amarkal.settings.notifier.error(error);
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
18
        }
19
        else {
20
            Amarkal.settings.notifier.success('Settings saved', 2000);
21
        }
22
23
        Amarkal.settings._updateValues(res.values);
24
        
25
        done();
26
    });
27
};
28
29
/**
30
 * Asynchronously reset all settings in the current settings page to their 
31
 * default values and erase all data from the database. Shows a success 
32
 * notification upon completion.
33
 * 
34
 * @param {Function} done
35
 */
36
Amarkal.settings.reset = function( done ) 
37
{
38
    Amarkal.settings._postData('reset',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
39
        
40
        Amarkal.settings.notifier.success('Default settings applied', 2000);
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
        Amarkal.settings._updateValues(res.values);
42
        
43
        done();
44
    });
45
};
46
47
/**
48
 * Update all components in the current settings page with the given values.
49
 * 
50
 * @param {Array} values
51
 */
52
Amarkal.settings._updateValues = function( values )
53
{
54
    for(var name in values) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
55
        var value = values[name],
56
            $comp = $('[amarkal-component-name="'+name+'"]');
57
        
58
        if($comp.hasClass('amarkal-ui-component')) {
59
            $comp.amarkalUIcomponent().setValue(value);
60
        }
61
    }
62
};
63
64
/**
65
 * Send serialized form data to be processed in the backend by the function given
66
 * in the 'action' variable.
67
 * 
68
 * @param {string} action
69
 * @param {Function} done
70
 */
71
Amarkal.settings._postData = function( action, done )
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
72
{
73
    $.post(ajaxurl, {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
74
        action: 'amarkal_settings_'+action,
75
        data: $('#amarkal-settings-form').serialize()
76
    }, done);
77
};